package jp.jawsug.account;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Activity implements List<ActivityItem> {

    private ArrayList<ActivityItem> activities = new ArrayList<ActivityItem>();

    public static void main(String[] args) {
        if (args.length >= 2) {
            ActivityClient ac = new ActivityClient(new MailPassAWSCredentials(args[0], args[1]));
            Activity activity = ac.get();
            for (ActivityItem item : activity) {
                if (args.length >= 3) {
                    
                    if (item.toString().matches(".*?"+args[2]+".*")) {
                        System.out.println(item.toString());
                    }
                }else{
                    System.out.println(item.toString());
                }
            }
        }
    }

    public static Activity factory(CharSequence body) {
        // 本当は全部パースして、サービス毎に出せるようにしたかったんだ。。。
        return Activity.parse(body);
    }

    private static Activity parse(CharSequence body) {
        Pattern p = Pattern.compile("(<tr[^>]*?id=\"([^0-9]+?.?)[0-9]{12}item.*?\".*?>(.*?)</tr>)");
        Matcher m = p.matcher(body);
        Activity activity = new Activity();
        String service = "";
        String rawdata = "";
        String region = "";
        String subtitle = "";
        while (m.find()) {
            rawdata = m.group(1);
            if (!service.equals(m.group(2))) {
                subtitle = "";
                region = "";
            }
            service = m.group(2).trim();
            String tds = m.group(3).trim();
            if (tds.matches(".*?colspan=\"5\".*?")) {
                // リージョン
                region = tds.replaceAll(".*?<td.*?>(.*?)</td>.*?", "$1").trim();
                continue;
            }
            if (tds.matches("colspan=\"4\"")) {
                subtitle = tds.replaceAll("<.*?>", tds).trim();
                continue;
            }
            if (tds.matches(".*?(bgyel|bggrey).*?")) {
                // 合計行
                // 特に何もしない
                continue;
            }
            String[] cell = tds.replaceAll("(<td.*?>|\t|&nbsp;)", "").split("</td>");
            if (cell.length == 5) {
                subtitle = cell[1].trim();
                String range = cell[2].trim();
                String commodity = cell[3].trim();
                String price = cell[4].trim();
                activity.put(service, region, subtitle, range, commodity, price, rawdata);
            }

        }
        return activity;
    }

    private Activity() {

    }

    /**
     * @deprecated
     * @return
     */
    public float getTotal() {
        float total = 0;
        for (ActivityItem item : this.activities) {
            try {
                total += Float.parseFloat(item.getPrice());
            }
            catch (NumberFormatException e) {

            }
        }
        return total;
    }

    public void put(String service, String region, String subtitle, String range, String commodity, String price, String rawdata) {
        this.activities.add(new ActivityItem(service, region, subtitle, range, commodity, price, rawdata));
    }

    public boolean add(ActivityItem e) {
        return this.activities.add(e);
    }

    public void add(int index, ActivityItem element) {
        this.activities.add(index, element);
    }

    public boolean addAll(Collection<? extends ActivityItem> c) {
        return this.activities.addAll(c);
    }

    public boolean addAll(int index, Collection<? extends ActivityItem> c) {
        return this.activities.addAll(index, c);
    }

    public void clear() {
        this.activities.clear();
    }

    public Object clone() {
        return this.activities.clone();
    }

    public boolean contains(Object o) {
        return this.activities.contains(o);
    }

    public boolean containsAll(Collection<?> arg0) {
        return this.activities.containsAll(arg0);
    }

    public void ensureCapacity(int minCapacity) {
        this.activities.ensureCapacity(minCapacity);
    }

    public boolean equals(Object arg0) {
        return this.activities.equals(arg0);
    }

    public ActivityItem get(int index) {
        return this.activities.get(index);
    }

    public int hashCode() {
        return this.activities.hashCode();
    }

    public int indexOf(Object o) {
        return this.activities.indexOf(o);
    }

    public boolean isEmpty() {
        return this.activities.isEmpty();
    }

    public Iterator<ActivityItem> iterator() {
        return this.activities.iterator();
    }

    public int lastIndexOf(Object o) {
        return this.activities.lastIndexOf(o);
    }

    public ListIterator<ActivityItem> listIterator() {
        return this.activities.listIterator();
    }

    public ListIterator<ActivityItem> listIterator(int arg0) {
        return this.activities.listIterator(arg0);
    }

    public ActivityItem remove(int index) {
        return this.activities.remove(index);
    }

    public boolean remove(Object o) {
        return this.activities.remove(o);
    }

    public boolean removeAll(Collection<?> arg0) {
        return this.activities.removeAll(arg0);
    }

    public boolean retainAll(Collection<?> arg0) {
        return this.activities.retainAll(arg0);
    }

    public ActivityItem set(int index, ActivityItem element) {
        return this.activities.set(index, element);
    }

    public int size() {
        return this.activities.size();
    }

    public List<ActivityItem> subList(int arg0, int arg1) {
        return this.activities.subList(arg0, arg1);
    }

    public Object[] toArray() {
        return this.activities.toArray();
    }

    public <T> T[] toArray(T[] a) {
        return this.activities.toArray(a);
    }

    public String toString() {
        return this.activities.toString();
    }

    public void trimToSize() {
        this.activities.trimToSize();
    }

}
